package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.Book;
import com.itheima.service.IBookService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import com.itheima.common.R;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author szx
 * @since 2022-09-12
 */
@RestController
@RequestMapping("/book")

public class BookController {

    @Autowired
    IBookService bookService;

    /*//根据id查询
    @GetMapping("/{id}")
    public Book findallid(@PathVariable int id) {
        return bookService.getById(id);
    }*/

    //根据结果集id查询
    @GetMapping("/{id}")
    public R chaxun(@PathVariable Long id) {
        Book byId = bookService.getById(id);
        return R.success(byId);
    }

    /*//查询所有
    @GetMapping
    public List<Book> findall() {
        return bookService.list(null);
    }*/

    //根据结果集查询所有
    @GetMapping
    public R suoyou() {
        List<Book> list = bookService.list(null);
        return R.success(list);
    }

    /*//新增
    @PostMapping
    public Boolean zengjia(@RequestBody Book book) {
        boolean save = bookService.save(book);
        return save;
    }*/

    //根据结果集新增
    @PostMapping
    public R zengjia2(@RequestBody Book book) {
        boolean save = bookService.save(book);
        return save ? R.success("新增成功") : R.error("新增失败");
    }

    /*//根据id删除
    @DeleteMapping("/{id}")
    public boolean shan(@PathVariable Long id) {
        boolean b = bookService.removeById(id);
        return b;
    }*/

    //根据结果集id删除
    @DeleteMapping("/{id}")
    public R shanchu(@PathVariable Long id) {
        boolean b = bookService.removeById(id);
        return b ? R.success("删除成功") : R.error("删除失败");
    }
/*

    //修改数据
    @PutMapping
    public Boolean update(@RequestBody Book book) {

        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        // eq 等于
        //   参数1：判断条件为true就拼接该条件
        //   参数2：字段名
        //   参数3：值
        //
        queryWrapper.eq(book.getId() != null, Book::getId, book.getId());

        boolean i = bookService.update(book, queryWrapper);
        return i;
    }
*/

    //结果集修改数据
    @PutMapping
    public R update2(@RequestBody Book book) {

        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        // eq 等于
        //   参数1：判断条件为true就拼接该条件
        //   参数2：字段名
        //   参数3：值
        //
        queryWrapper.eq(book.getId() != null, Book::getId, book.getId());

        boolean i = bookService.update(book, queryWrapper);
        return i ? R.success("修改成功") : R.error("修改失败");
    }
}

